<?php
require_once(dirname(__FILE__).'/../../../../inc/class/server/ServerListType.php');
define("WF_EXITCODE_SUCCESS", 0);
define("WF_EXITCODE_WRONGPARAMETERS", 1);
define("WF_EXITCODE_UPDATEDBFAILED", 2);
define("WF_EXITCODE_ACCESSDBFAILED", 3);
define("WF_EXITCODE_QUERYDBFAILED", 4);

require_once(dirname(__FILE__).'/../../../../inc/class/init.php');
WF::initCLI($argv, $argc);

function setUnitTest()
{
	//define('IS_UNITTEST',true);
	WF::initUnitTest();
	global $wfconf_WFHome;
	$GLOBALS['wfconf_dbconfig']["dbfile"] = $wfconf_WFHome."/repository/db/sqlite/tmwf_test.db"; 
}
function ShowHelp() {
	echo " 
This is Trend Micro Widget Framework command line script for server list types.

  Usage:
  synServerListType.php <action> [options]

  <action> Action can be one of following. With the --help, -help, -h,
  or -? options, you can get this help.
  
  update [serverlist.xml path]: According to serverlist.xml, update server list types
    
  print: List current supported server list types
  
  localhost: Get the localhost server type
";
}
if ($argc < 2 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
	ShowHelp();
	exit(WF_EXITCODE_WRONGPARAMETERS);
} else {
	if(true === WF::isUnitTest())
	{
		setUnitTest();
	}
	switch($argv[1]){
		case "update":
		    $strAssignedserverlistxml = isset($argv[2]) ? $argv[2] : null;
			/*if(isset($argv[2]))
			{
				$objserverlist=new WFServerListType($argv[2]);	
			}
			else
			{
				$objserverlist=new WFServerListType();
			}
			$objserverlist->execute();
			if($objserverlist->isFailed())
			{
				exit(WF_EXITCODE_UPDATEDBFAILED);
			}*/
		    try{
		        WF::getWidgetFactory()->getInterface()->synServerListTypeUpdate($strAssignedserverlistxml);
		    }catch(Exception $e){
		        exit(WF_EXITCODE_UPDATEDBFAILED);
		    }
			exit(WF_EXITCODE_SUCCESS);
			break;
		case "print":
			// create ServerListType object
			$objServerListTypeDBobj = new WFServerListTypeDao( $GLOBALS['wfconf_dbconfig']);
			if( $objServerListTypeDBobj->isFailed() ) {
				exit(WF_EXITCODE_ACCESSDBFAILED);
			}
			$arrstypelist=$objServerListTypeDBobj->getall();
			if($arrstypelist !== FALSE)
			{
				if(true === WF::isUnitTest())
				{
					echo(json_encode($arrstypelist));
				}
				else
				{
					print_r($arrstypelist);
				}
				exit(WF_EXITCODE_SUCCESS);
	  		}
	  		else
	  		{
	  			exit(WF_EXITCODE_QUERYDBFAILED);
	  		}
	  		break;
		case "localhost":
			$objServerDBobj = new WFServerDao( $GLOBALS['wfconf_dbconfig'], 0);
			if( $objServerDBobj->isFailed() ) {
				exit(WF_EXITCODE_ACCESSDBFAILED);
				return;
			}
			$arrslist=$objServerDBobj->getall();
			if($arrslist !== FALSE)
			{
				if(isset($arrslist[0]) && isset($arrslist[0]["hostinfo"]) && ""!==$arrslist[0]["hostinfo"])
				{
					print($arrslist[0]["hostinfo"]["type"]);
				}
				else
				{
					print "";
				}
				exit(WF_EXITCODE_SUCCESS);
			}
			else
			{
				exit(WF_EXITCODE_QUERYDBFAILED);
			}
			break;
		default:
			ShowHelp();
			exit(WF_EXITCODE_WRONGPARAMETERS);
	}
}